package com.wolfword.crawler.server.controller;


import java.io.File;
import java.io.RandomAccessFile;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wolfword.common.domain.Constant;
import com.wolfword.common.domain.ResultMessage;
import com.wolfword.common.util.object.StringUtil;
import com.wolfword.crawler.server.entity.RootUrlInfoEntity;
import com.wolfword.crawler.server.service.CrawlerService;
import com.wolfword.crawler.server.service.RootUrlInfoService;


@RestController
@RequestMapping("/api")
public class CrawlerController {
	
	private final Logger logger = LoggerFactory.getLogger(CrawlerController.class);
	
	@Value("${html.store.path}")
	private String htmlStorePath;
	
	@Autowired
	private CrawlerService crawlerService;
	@Autowired
	private RootUrlInfoService rootUrlInfoService;
	
	/**
	 * 获取未爬取的根域名信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/rootUrl",method=RequestMethod.GET)
	public synchronized  ResultMessage getRootUrl(HttpServletRequest request) throws Exception{
		
		ResultMessage result = new ResultMessage();
		Map<String,Object> data = null;
		RootUrlInfoEntity entity = null;
		String message = null;
		try {
			/*data = crawlerService.getRootUrl();
			result.setCode(Constant.SUCCESS_CODE);
			message="获取未爬取根域名信息成功";*/
			/*entity = rootUrlInfoService.getUnClawled();
			result.setCode(Constant.SUCCESS_CODE);
			message="获取未爬取根域名信息成功";*/
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(Constant.ERROR_CODE);
			message=e.getMessage();
		}
		
		result.setMessage(message);
		result.setData(entity);
		
		logger.info("code:"+result.getCode());
		logger.info("message:"+result.getMessage());
		logger.info("data:"+result.getData());
		
	    return result;
	}
	/**
	 * 上传文件
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/upload-files",method=RequestMethod.POST)
	public String uploadFile(@RequestParam("name") String name,@RequestParam("contain_str") String contain_str,HttpServletRequest request) {
		if (StringUtil.isEmpty(htmlStorePath)) {
			htmlStorePath = "./htmls/";
		}
		String message=null;
		try{
			String path = htmlStorePath+contain_str.replace(".", "_")+"/"+name.substring(0,1)+"/"+name.substring(1,2)+"/"+name.substring(2,3);
			File f = new File(path);
			if(!f.exists()) 
				f.mkdirs();
			
			ServletInputStream sis = request.getInputStream();
			
			RandomAccessFile file=new RandomAccessFile(path+"/"+name,"rw");
			
			byte[] buf=new byte[1024];
			int len = sis.read(buf,0,1024);
			while(len!=-1){
				file.write(buf,0,len);
				len=sis.read(buf,0,1024);
			}
			file.close();
			sis.close();
			message="上传成功";
		}catch(Exception e){
			logger.info("上传异常");
			message="上传异常";
			e.printStackTrace();
		}
		
	    return message;
	}
	/**
	 * 统计子网页信息和parent_url_id
	 * @param url
	 * @return
	 * @throws Exception
	 */
	/*@RequestMapping(value="/statistics",method=RequestMethod.POST)
	public synchronized  ResultMessage statisticUrl(@RequestParam("url") String url) throws Exception{
		ResultMessage result = new ResultMessage();
		String data = null;
		String message = null;
		try {
			data = crawlerService.statisticUrl(url);
			result.setCode(Constant.SUCCESS_CODE);
			message="统计子网页信息成功";
				
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(Constant.ERROR_CODE);
			message=e.getMessage();
		}
		result.setMessage(message);
		result.setData(data);
	    return result;
	}*/

}
